package com.anbao.newanbao;

import com.anbao.action.ActivityManagerAction;
import com.anbao.server.SeocketServer;
import com.anbao.server.SoketClient;
import com.anbao.util.SystemUtil;
import com.anbao.util.FileUtil;
import com.anbao.util.CommonData;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Toast;

public class WelcomeActiviy extends Activity {

	
	private boolean socketRunstate = false;
	
	public boolean iniSystemConfig = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		
		iniSystem();
		
		startServer();
	
		
		
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	
		registerBoradcastReceiver();
		
		 Intent mIntent = new Intent(CommonData.CONNECTSCOKETACTION);  
           //发送广播  
		 WelcomeActiviy.this.sendBroadcast(mIntent); 
		 
		// hide statusbar of Android
		
		
		// could also be done later
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		// 将当前界面加入到系统管理中国
		ActivityManagerAction.getScreenManager().pushActivity(this);
		
		View view = getLayoutInflater().inflate(R.layout.activity_splash, null);
		
		AlphaAnimation animation = new AlphaAnimation(1.0f, 1.0f);
		animation.setDuration(1000);
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				
				skip();	
				
				if(socketRunstate){
					
					skip();
				}else{
					
					new AlertDialog.Builder(WelcomeActiviy.this).setTitle("服务器连接").setMessage("未成功连接服务器")  
					.setPositiveButton("确定",new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							stopServer();
							WelcomeActiviy.this.finish();
						}
					} )  .show(); 
					
				}
		
			}
		});
		view.setAnimation(animation);
		setContentView(view);
		
		
	
		
	}
	
	/**
	 * 初始化参数
	 */
	private void iniSystem() {
		
		//读取系统配置文件
		FileUtil.readSystemConfig();
		//表示没有读取到系统的配置文件
		if(SystemUtil.usrId==null||SystemUtil.pwd==null){
			iniSystemConfig = false;
		}else{
			iniSystemConfig = true;
		}
		
	}

	private void startServer() {
		
		Intent intent = new Intent(WelcomeActiviy.this, SeocketServer.class);
		startService(intent);
		
	}
	
	
	private void stopServer() {
		
		Intent intent = new Intent(WelcomeActiviy.this, SeocketServer.class);
		stopService(intent);
		
	}

	private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(CommonData.CONNECTSCOKETACTIONRESULT)) {
				
				socketRunstate = intent.getExtras().getBoolean("data");

			} 
		}

	};

	public void registerBoradcastReceiver() {
		IntentFilter myIntentFilter = new IntentFilter();
		// 注册修改密码广播
		myIntentFilter.addAction(CommonData.CONNECTSCOKETACTIONRESULT);
		// 注册广播
		registerReceiver(mBroadcastReceiver, myIntentFilter);
	}
	

	private void skip() {
		Intent intent = new Intent(this, LoginActivity.class);
		startActivity(intent);
		finish();
	}
}